using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Collections;
using System.Security.Cryptography;


namespace GlobalDefine
{
    public class VariableDefine
    {
        public static string strAppPath;
        public static string strConfLocation;
        public static string strPicLocation;
        public static string strUsers;
        public static string strRates;
        public static string strGroups;
        


        public static string getDefinefromINI(string strVariable)
        {
            try
            {
                string strReturnVariable=null;
                using (StreamReader sr = File.OpenText(strConfLocation))
                {
                    while (!sr.EndOfStream)
                    {
                        string strTmp = sr.ReadLine();
                        if (strTmp.IndexOf(@"[" + strVariable + @"]") == 0)
                            strReturnVariable = strTmp.Split('=')[1].ToString();
                    }
                    sr.Close();
                }
                return strReturnVariable;
            }
            catch
            {
                return null;
            }
        }
        public static bool saveDefine2INI(string strVariable, string strValue)
        {
            try
            {
                string strAllVariable = "";
                bool isNewer = true;
                using (StreamReader sr = File.OpenText(strConfLocation))
                {
                    while (!sr.EndOfStream)
                    {
                        string strTmp = sr.ReadLine().Trim();
                        if (strTmp.IndexOf(@"[" + strVariable + @"]") == 0)
                        {
                            strTmp = @"[" + strVariable + @"]=" + strValue.Trim();
                            isNewer = false;
                        }
                        strAllVariable = strAllVariable + strTmp + "/n/r";
                        if (isNewer)
                            strAllVariable = strAllVariable + @"[" + strVariable + @"]=" + strValue.Trim();
                    }
                    sr.Close();
                }

                using (StreamWriter sw = new StreamWriter(strConfLocation,false))
                {
                    sw.Write(strAllVariable);
                    sw.Close();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}